$file_mrna="/data4/bsi/bioinf_ext1/s113861.chrX/TCGA_DATA/MRNAseq/code_merge/BRCA_normalized_mrnaSEQv2_tumor_normal_only.txt";
$file_methylatiton="/data4/bsi/bioinf_ext1/s113861.chrX/TCGA_DATA/Methylation_450/code_merge/BRCA_normalized_450_methylation_tumor_normal_only.txt";
$file_mrna_wr="/data4/bsi/bioinf_ext1/s113861.chrX/TCGA_DATA/common/WR_BRCA_normalized_mrnaSEQv2_tumor_normal_only.txt";
$file_methylatiton_wr="/data4/bsi/bioinf_ext1/s113861.chrX/TCGA_DATA/common/WR_BRCA_normalized_450_methylation_tumor_normal_only.txt";
open(MRNA,$file_mrna) or die "no file $file_mrna found\n";
open(METHYLATION,$file_methylatiton) or die "no file $file_methylatiton found\n";
open(WRMRNA,">$file_mrna_wr") or die "no file $file_mrna_wr\n";
open(WRMETHY,">$file_methylatiton_wr") or die "no file $file_methylatiton_wr\n";
$header_mrna=<MRNA>;
chomp($header_mrna);
#print $header."\n";
@header_mrna=split("\t",$header_mrna);
for($i=0;$i<@header_mrna;$i++)
{
	@names=split("-",$header_mrna[$i]);
	$hash{"$names[0]-$names[1]-$names[2]"}=1;
	#print "$header_mrna[$i]\t$names[0]-$names[1]-$names[2]\n";
	#die "$names[0]-$names[1]-$names[2]\n";
}
$header_methy=<METHYLATION>;
chomp($header_methy);
@header_methy=split("\t",$header_methy);
#$header_methy =~ s/\n//g;
#print $header_methy."\n";
@samples=();
@header=();
for($i=0;$i<@header_methy;$i++)
{
    @names=split("-",$header_methy[$i]);
	#die "$names[0]-$names[1]-$names[2]\n";
	if(exists($hash{"$names[0]-$names[1]-$names[2]"}))
	{
		push(@samples,$i);
		#$header_methy[$i] =~ s/\n//g;
		push(@header,$header_methy[$i]);
		$methy{"$names[0]-$names[1]-$names[2]"}=1;
		#print $header_methy[$i]."\n";
	}
}
$header=join("\t",@header);
print WRMETHY $header."\n";
while(<METHYLATION>)
{
	chomp($_);
	if($_ =~ m/\w/)
	{
		@methy=split("\t",$_);
		print WRMETHY $methy[0];
		for($i=0;$i<@samples;$i++)
		{
			$temp=$samples[$i]+1;
			print WRMETHY "\t$methy[$temp]";
		}
		print WRMETHY "\n";
		#die;
	}
}

undef(@samples);
undef(@header);
@samples=();
@header=();
for($i=0;$i<@header_mrna;$i++)
{
    @names=split("-",$header_mrna[$i]);
	#die "$names[0]-$names[1]-$names[2]\n";
	if(exists($methy{"$names[0]-$names[1]-$names[2]"}))
	{
		push(@samples,$i);
		#$header_methy[$i] =~ s/\n//g;
		push(@header,$header_mrna[$i]);
		$methy{"$names[0]-$names[1]-$names[2]"}=1;
		#print $header_methy[$i]."\n";
	}
}
$header=join("\t",@header);
print WRMRNA $header."\n";
while(<MRNA>)
{
	chomp($_);
	@mrna=split("\t",$_);
	print WRMRNA $mrna[0];
	for($i=0;$i<@samples;$i++)
	{
		$temp=$samples[$i]+1;
		print WRMRNA "\t$mrna[$temp]";
	}
	print WRMRNA "\n";
}
